<template>
  <div>
    <div>
      <BasicTable @register="registerTable" @row-click="handleTestDataChildRowClick">
        <!-- 表格标题 -->
        <template #tableTitle>
          <Icon :icon="getTitle.icon" class="m-1 pr-1" />
          <span> {{ getTitle.value }} </span>
        </template>
        <!-- 表格右上角自定义按钮（新增...） -->
        <template #toolbar>
          <div>
            <a-button
              v-if="!props.data.flag || props.data.flag == '0'"
              type="default"
              @click="handleForm({ routeType: '0' })"
              class="m-1 pr-1"
              v-auth="'bas:route:edit'"
            >
              <Icon icon="fluent:add-12-filled" /> {{ t('常规工艺') }}
            </a-button>
            <a-button
              v-if="!props.data.flag || props.data.flag == '1'"
              type="default"
              @click="handleForm({ routeType: '1' })"
              class="m-1 pr-1"
              v-auth="'bas:route:edit'"
            >
              <Icon icon="fluent:add-12-filled" /> {{ t('数铣工艺') }}
            </a-button>
          </div>
        </template>
        <template #firstColumn="{ record }">
          <a @click="handleForm({ id: record.id })">
            {{ record.testInput }}
          </a>
        </template>
      </BasicTable>
      <!-- 常规工艺 / 数铣工艺 -->
      <InputForm @register="registerDrawer" @success="handleSuccess" />
      <!-- 数铣工艺路线 -->
      <!-- <InputFormTabs @register="registerDrawer2" @success="handleSuccess" /> -->
    </div>
    <!-- 子表 -->
    <div>
      <BasicTable @register="registerTable2">
        <!-- 表格标题 -->
        <template #tableTitle>
          <Icon :icon="getTitle2.icon" class="m-1 pr-1" />
          <span> {{ getTitle2.value }} </span>
        </template>
        <!-- 表格右上角自定义按钮（新增...） -->
        <!-- <template #toolbar>
        <a-button type="primary" @click="handleForm2({})" v-auth="'bas:route:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template> -->
        <template #firstColumn="{ record }">
          <a @click="handleForm2({ id: record.id })">
            {{ record.testInput }}
          </a>
        </template>
      </BasicTable>
      <!-- 基础编辑新增 -->
      <InputForm @register="registerDrawer" @success="handleSuccess2" />
    </div>

    <RouteForm @register="registerDrawerRoute" @success="handleSuccess" />
    <SxForm @register="registerDrawerSxRoute" @success="handleSuccess" />
  </div>
</template>

<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasRouteList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, watch, ref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { routeDelete, routeListData, routeChildListData } from '/@/api/bas/route/route';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  import RouteForm from '/@/views/m8/order/pic/m8OrderRoute/form.vue';
  import SxForm from '/@/views/m8/order/flow/flow/sxForm.vue';

  const emit = defineEmits(['success']);

  const { t } = useI18n('test.testData');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('工艺路线管理'),
  };
  const getTitle2 = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('工序明细列表'),
  };

  const props = defineProps({
    data: { type: Object, default: {} },
  });
  watch(
    () => props.data,
    () => {},
    { immediate: true },
  );
  let routeId = ref<string>('');

  const [registerDrawerRoute, { openDrawer: openDrawerRoute }] = useDrawer();
  const [registerDrawerSxRoute, { openDrawer: openDrawerSxRoute }] = useDrawer();

  function openRouteForm() {
    if (routeId.value) {
      if (props.data.flag == '0') {
        let obj = {
          routeId: routeId.value,
          orderId: props.data.order.id,
          id: props.data.id,
        };
        openDrawerRoute(true, obj);
      } else {
        let sx = {
          p_routeId: routeId.value,
          id: props.data.orderPic.id,
          sxManCode: props.data.sxManCode,
          sxManName: props.data.sxManName,
        };
        openDrawerSxRoute(true, sx);
        emit('success', '');
      }
    } else {
      showMessage('请选择一个工艺！');
    }
  }

  //配置表单内容
  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工艺类型'),
        field: 'routeType',
        component: 'Select',
        defaultValue: props.data.flag,
        dynamicDisabled: props.data.flag == undefined || props.data.flag == '' ? false : true,
        componentProps: {
          maxlength: 200,
          dictType: 'bas_route_type',
        },
      },
      {
        label: t('工艺编码'),
        field: 'routeCode',
        component: 'Input',
      },
      {
        label: t('工艺名称'),
        field: 'routeName',
        component: 'Input',
      },
      {
        label: t('工艺说明'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  //配置表格表头菜单
  const tableColumns: BasicColumn[] = [
    {
      title: t('工艺类型'),
      dataIndex: 'routeType',
      key: 'a.route_type',
      sorter: true,
      width: 100,
      dictType: 'bas_route_type',
    },
    {
      title: t('工艺编码'),
      dataIndex: 'routeCode',
      key: 'a.route_code',
      sorter: true,
      width: 120,
      align: 'center',
      // slot: 'firstColumn',
    },
    {
      title: t('工艺名称'),
      dataIndex: 'routeName',
      key: 'a.route_name',
      sorter: true,
      width: 350,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('工艺说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 350,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('创建人'),
      dataIndex: 'creator.userName',
      key: 'a.create_by',
      sorter: true,
      width: 130,
      align: 'center',
      defaultHidden: props.data.picno ? true : false,
      // slot: 'firstColumn',
    },
  ];

  const tableColumns2: BasicColumn[] = [
    {
      title: t('顺序号'),
      dataIndex: 'sortNum',
      key: 'a.sort_num',
      sorter: true,
      width: 80,
      align: 'center',
      // slot: 'firstColumn',
    },
    {
      title: t('工序'),
      dataIndex: 'oper.operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 120,
      align: 'center',
      // slot: 'firstColumn',
    },
    {
      title: t('工序名称'),
      dataIndex: 'oper.operName',
      key: 'oper.oper_name',
      sorter: true,
      width: 150,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('加工时长'),
      dataIndex: 'jgtime',
      key: 'a.jgTime',
      sorter: true,
      width: 80,
      align: 'center',
      // slot: 'firstColumn',
    },
    {
      title: t('调试时长'),
      dataIndex: 'tstime',
      key: 'a.tsTime',
      sorter: true,
      width: 80,
      align: 'center',
      // slot: 'firstColumn',
    },
    {
      title: t('单价'),
      dataIndex: 'fprice',
      key: 'a.fprice',
      sorter: true,
      width: 80,
      align: 'center',
      // slot: 'firstColumn',
    },
    {
      title: t('班次数'),
      dataIndex: 'teamQty',
      key: 'a.team_qty',
      sorter: true,
      width: 80,
      align: 'center',
      // slot: 'firstColumn',
    },
  ];

  //配置表格右边操作按钮
  const actionColumn: BasicColumn = {
    width: 80,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'bas:route:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除数据'),
        popConfirm: {
          title: t('是否确认删除数据'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'bas:route:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, setProps }] = useTable({
    api: routeListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns, //配置表格内容数组对象
    formConfig: searchForm, //配置表单内容
    showTableSetting: true, //表格右上角3个默认按钮
    useSearchForm: true, //表单是否展示
    canResize: true, //表格是否flex布局
    scroll: {
      y: 250,
    },
    rowSelection: {
      type: 'radio',
    },
  });

  onMounted(() => {
    if (!props.data.picno) {
      setProps({
        actionColumn: actionColumn,
      });
    }
  });

  const [registerTable2, { reload: reload2 }] = useTable({
    api: routeChildListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns2, //配置表格内容数组对象
    // actionColumn: actionColumn, //配置表格右边操作按钮
    // formConfig: searchForm, //配置表单内容
    // showTableSetting: true, //表格右上角3个默认按钮
    // useSearchForm: true, //表单是否展示
    showIndexColumn: false,
    canResize: true, //表格是否flex布局
    maxHeight: 400,
    immediate: false, //懒加载
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  // 鼠标行点击事件，获取上表数据
  function handleTestDataChildRowClick(record: Recordable) {
    routeId.value = record.id;
    record.onEdit?.(true, false);
    reload2({
      searchInfo: { 'route.id': record.id },
    });
  }

  async function handleDelete(record: Recordable) {
    const res = await routeDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    emit('success');
    reload();
  }

  function handleSuccess2() {
    reload2();
  }
  defineExpose({
    openRouteForm,
  });
</script>
